<template>
  <div class="app-container">
    <!-- 医院列表设置 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
          <el-input v-model="searchObj.hosname" placeholder="医院名称"></el-input>
      </el-form-item>
      <el-form-item>
          <el-input v-model="searchObj.hoscode" placeholder="医院编号"></el-input>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 工具条 -->
    <div>
      <el-button type="danger" size="mini" @click="removeByIdList()">批量删除</el-button>
    </div>
    <el-table
      :data="list"
      stripe
      style="width: 100%"
      @selection-change="handleSelection">
      <el-table-column type="selection" width="50" />
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column prop="hosname" label="医院名称" width="160" />
      <el-table-column prop="hoscode" label="医院编号" width="160"  />
      <el-table-column prop="apiUrl" label="api基础路径" width="200" />
      <el-table-column prop="contactsName" label="联系人姓名" width="160"  />
      <el-table-column prop="contactsPhone" label="联系人手机" width="160"  />
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "可用" : "不可用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini"
          icon="el-icon-delete" @click="removeById(scope.row.id)">删除</el-button>
          <router-link :to="'/hospital/edit/'+scope.row.id" >
            <el-button type="primary" size="mini" class="margin10"
            icon="el-icon-edit">编辑</el-button>
          </router-link>
          <el-button v-if="scope.row.status==1" type="warning" size="mini"
          icon="el-icon-lock" @click="lockHospSet(scope.row.id, 0)">锁定</el-button>
          <el-button v-if="scope.row.status==0" type="success" size="mini"
          icon="el-icon-unlock" @click="lockHospSet(scope.row.id, 1)">取消锁定</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"/>
  </div>
</template>

<style>
  .margin10 {
    margin-left: 10px;
    margin-right: 10px;
  }
</style>

<script>
// 引入js文件
import hospitalset from '@/api/hospitalset'

export default {
  // 定义初始值和变量
  data() {
    return {
      current: 1, // 当前页
      limit: 5, // 每页数量
      searchObj: {}, // 条件查询对象
      list: [], // 每页数据集合
      total: 0, // 总记录数
      selectedIds: [] // 选中的数据
    }
  },
  // 在页面渲染之前执行
  created() {
    // 一般调用methods定义的方法，得到数据
    this.getList()
  },
  // 定义方法。进行请求接口调用
  methods: {
    // 医院设置列表
    getList(page = 1) {
      this.current = page
      hospitalset.getHospitalSetList(this.current, this.limit, this.searchObj)
        .then((response) => {
          // 请求成功后调用
          // console.log(response)
          this.list = response.data.records
          this.total = response.data.total
          console.log(this.list)
        })
        .catch((error) => {
          // 请求失败后调用
          console.log(error)
        })
    },
    //根据id删除数据
    removeById(id) {
      this.$confirm('确认删除数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {//点击确定按钮
        //调用接口删除数据
        hospitalset.deleteHospSetById(id)
        .then((response) => {
          //提示信息
          this.$message({
            type: 'success',
            message: '删除成功！'
          })
          //刷新列表数据
          this.getList(1)
        })
        .catch((error) => {
          // 请求失败后调用
          console.log(error)
        })
      }).catch((error) => {
        
      })
    },
    //获取复选框选中的值
    handleSelection(selection) {
      this.selectedIds = []
      for(var i = 0; i < selection.length; i++) {
        this.selectedIds.push(selection[i].id)
      }
    },
    //批量删除
    removeByIdList() {
      if (this.selectedIds.length == 0) {
        this.$message({
          message: '请先选择要删除的数据',
          type: 'warning'
        })
        return
      }
      this.$confirm('确认删除数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {//点击确定按钮
        //调用接口删除数据
        hospitalset.batchDeleteHospSet(this.selectedIds)
        .then((response) => {
          //提示信息
          this.$message({
            type: 'success',
            message: '删除成功！'
          })
          //刷新列表数据
          this.getList(1)
        })
        .catch((error) => {
          // 请求失败后调用
          console.log(error)
        })
      }).catch((error) => {

      })
    },
    //锁定或取消锁定
    lockHospSet(id, status) {
      hospitalset.lockHospSet(id, status)
      .then((response) => {
        //刷新列表
        this.getList(this.current)
      })
    },
    //清空数据
    resetData() {
            this.searchObj = {}
        }
  }
}
</script>
